package src.view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Map;
import java.util.Set;

import javax.imageio.ImageIO;

import src.event.EventListener;
import src.event.events.SelectionUpdateEvent;
import src.model.gameWorld.GameDataObject;
import src.model.gameWorld.Position;
import src.view.ViewHandler.DisplayType;
import src.view.ViewHandler.ImageType;

/**
 * <b>Overview</b>
 * The StatusViewport shows the currently selected unit or structure as well as relevant information about it.  It also shows links to other viewports.
 *
 * <b>Responsibilities</b>
 * Display the currently selected object and its relevant statistics as well as links to other views.  These links are mouse-accessible.
 *
 * <b>Collaborators</b>
 * Drawable - Subcontractor: The Drawable object dislays the viewport to the screen.
 *
 * <b>Implementor</b> - Bruce
 * <b>Tester</b> - Tim
 *
 * @author Bruce
 */
public class StatusViewport extends Viewport
{
	private GameDataObject [] selection;

	private ViewHandler vh;

	/**
	 * Creates a new StatusViewport.
	 *
	 * @param canvas the Drawable object on which to display the status view.
	 */
	public StatusViewport(ViewHandler vh, Drawable canvas)
	{
		super(vh, canvas, DisplayType.MAIN);
		this.vh = vh;
		// Background
		try {
		BufferedImage image = getImage(ImageType.WOOD);
		AffineTransform at = new AffineTransform();
		at.scale((double)getWidth() / (double)image.getWidth(), (double)getHeight() / (double)image.getHeight());
		getCanvas().drawImage(image, at, null);
		}catch(Exception e) {}
	}

	/* (non-Javadoc)
	 * @see src.view.Viewport#draw()
	 */
	@Override
	public void draw()
	{
		// Parchment
		BufferedImage image = getImage(ImageType.PARCHMENT);
		AffineTransform at = new AffineTransform();
		at.scale((double)getWidth() / (double)image.getWidth(), (double)getHeight() / (double)image.getHeight());
		getCanvas().drawImage(image, at, null);

		image = getImage(ImageType.B_UNIT);
		getCanvas().drawImage(image, 50, 400, null);
		image = getImage(ImageType.B_STRUCT);
		getCanvas().drawImage(image, 50, 440, null);
		image = getImage(ImageType.B_TECH);
		getCanvas().drawImage(image, 50, 480, null);

		int i = 0, y = 0;
		if(selection != null)
		{
			for(GameDataObject o : selection)
			{
				for(String key : o.getAllInfo().keySet())
					drawString(o.getInfo(key), 40, 100 + y * 100 + (++i * 20));
			}
			if(selection.length == 0)
				drawString("No selected objects", 30, 120);
		}
	}

	/* (non-Javadoc)
	 * @see src.view.Viewport#getResizeDimension()
	 */
	@Override
	protected Dimension getResizeDimension()
	{
		return new Dimension(200, getCanvasDimensions().height);
	}

	/* (non-Javadoc)
	 * @see src.view.Viewport#getResizePoint()
	 */
	@Override
	protected Point2D getResizePoint()
	{
		return new Point2D(getCanvasDimensions().width - 200, 0);
	}

	public void updateSelection(GameDataObject [] selection)
	{
		this.selection = selection;

		refresh(DisplayType.MAIN);
	}

	public String toString()
	{
		return "Status";
	}

	public void click(Point p)
	{
		int x = (int)p.getX();
		int y = (int)p.getY();

		if(x <= 150 && x >= 50)
		{
			if(y <= 510 && y >= 480)
				vh.setDisplay(DisplayType.TECHNOLOGY);
			else if(y <= 470 && y >= 440)
				vh.setDisplay(DisplayType.STRUCTURE);
			else if(y <= 430 && y >= 400)
				vh.setDisplay(DisplayType.UNIT);
		}
	}
}